package com.wang.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wang.dao.impl.StudentDaoImpl;
import com.wang.domain.Student;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final StudentDaoImpl studentDaoImpl;
	static{
		studentDaoImpl = new StudentDaoImpl();
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.sendRedirect("/home/jsp/login/login.jsp");
		request.getRequestDispatcher("/jsp/login/login.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Student student = studentDaoImpl.findByName(username);
		if(student == null){
			request.setAttribute("error", "账号都莫得！你娃想干嘛???");
			doGet(request,response);
			return;
		}else if(!password.equals(student.getPassword())){
			request.setAttribute("error", "账号都莫得！你娃想干嘛???");
			doGet(request,response);
			return;
		}else{
			HttpSession session = request.getSession();
			session.setAttribute("LOGIN_IN_SESSION", student);
			response.sendRedirect("/home/list");
		}
		
	}

}
